import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_detail.dart';
import 'package:enjoy_plus/pages/house/house_form.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/location_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/index.dart';
import 'package:enjoy_plus/pages/notice/index.dart';
import 'package:enjoy_plus/pages/profile/index.dart';
import 'package:enjoy_plus/pages/tab_bar_page.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

// GlobalKey 的作用是什么
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() {
  runApp(
    MaterialApp(
      navigatorKey: navigatorKey,
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => const NoticeDetail(),
        '/login': (context) => const LoginPage(),
      },
      initialRoute: '/',
      onGenerateRoute: (settings) {
        // 获取 token
        final token = TokenManager().getToken();
        // 如果 token 为空，跳转到登录页
        if (token == null) {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        } else if (settings.name == '/profile') {
          // print(settings.arguments);
          return MaterialPageRoute(
            builder:
                (context) => ProfilePage(userInfo: settings.arguments as Map),
          );
        } else if (settings.name == '/house_list') {
          return MaterialPageRoute(builder: (context) => const HouseList());
        } else if (settings.name == '/location_list') {
          return MaterialPageRoute(builder: (context) => const LocationList());
        } else if (settings.name == '/building_list') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => BuildingList(point: params['point']),
          );
        } else if (settings.name == '/room_list') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder:
                (context) =>
                    RoomList(point: params['point'], building: params['build']),
          );
        } else if (settings.name == '/house_form') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => HouseForm(params: params),
          );
        } else if (settings.name == '/house_detail') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => HouseDetail(id: params['id']),
          );
        }

        return null;
      },
    ),
  );
}
